package com.chinackts.auth.repository.redis;

import com.chinackts.auth.model.RdTime;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author Lianhong
 * @since 2021/11/1
 */
@Repository
public class RdTimeRepository {

    @Resource
    private StringRedisTemplate redisTemplate1;

    private static final String KEY = "%s:rdtime:%s";

    @Value("${e-port.redis.timeout}")
    private long timeout;

    /**
     * 获取rdtime
     */
    public String getRdTime(String id, RdTime type) {
        ValueOperations<String, String> ops = this.redisTemplate1.opsForValue();
        String menuType = ops.get(String.format(KEY, id, type.getCode()));
        if (StringUtils.isEmpty(menuType)) return null;
        return menuType;
    }

    /**
     * 保存rdtime
     */
    public void saveRdTime(String id, String rdTime, RdTime type) {
        ValueOperations<String, String> ops = this.redisTemplate1.opsForValue();
        ops.set(String.format(KEY, id, type.getCode()), rdTime, timeout, TimeUnit.HOURS);
    }

    /**
     * 是否已存在rdtime
     */
    public boolean exists(String id, RdTime type) {
        return Boolean.TRUE.equals(redisTemplate1.hasKey(String.format(KEY, id, type.getCode())));
    }
}
